Mock.mock("/js/huixianshangpin", "post", function () {
    // 获取本地存储
    let teacarString = localStorage.TeaCar;
    let teacatobj = JSON.parse(teacarString);
    return teacatobj;
})

Mock.mock("/js/huixianshangpin1", "post", function (obj) {
    // console.log(obj);

    let bodyObj = JSON.parse(obj.body);

    //获取本地存储
    let teaString = localStorage.Tea;
    let teaobj = JSON.parse(teaString)

    let arr = []
    for (let i = 0; i < teaobj.length; i++) {
        if (teaobj[i].teaType == bodyObj.teaType && teaobj[i].teaName != bodyObj.teaName) {
            arr.push(teaobj[i])
        }
    }
    return arr
})


class Tea {
    constructor(id, teaName, teaType, teaPrice, teaImg, num) {
        this.id = id;
        this.teaName = teaName;
        this.teaType = teaType;
        this.teaPrice = teaPrice;
        this.teaImg = teaImg;
        this.num = num;
    }
}

Mock.mock("/js/jrgwc", "post", function (obj) {
    let bodyObj = JSON.parse(obj.body);

    // 获取本地存储
    let teaString = localStorage.Tea;
    var teaobj = JSON.parse(teaString)
    let xuhao;
    for (let i = 0; i < teaobj.length; i++) {
        if (bodyObj.id == teaobj[i].id) {
            xuhao = i;
            break;
        }
    }
    //1.获取购物车本地存储 
    let carString = localStorage.Car;
    
    //判断本地存储是否存在
    if (carString == "" || carString == null || carString == undefined || carString == []) {
        let tea = new Tea(teaobj[xuhao].id, teaobj[xuhao].teaName, teaobj[xuhao].teaType, teaobj[xuhao].teaPrice, teaobj[xuhao].teaImg, bodyObj.num);
        let arr = [tea]
        localStorage.Car = JSON.stringify(arr)
        return true;
    } else {
        let carobj = JSON.parse(carString);
        console.log(carobj);
        // 设置标记值
        let bool = false;
        for (let i = 0; i < carobj.length; i++) {
            if (carobj[i].id == bodyObj.id) {
                bool = true;
                break
            }
        }
        if (bool) {
            return false;
        } else {
            let tea = new Tea(teaobj[xuhao].id, teaobj[xuhao].teaName, teaobj[xuhao].teaType, teaobj[xuhao].teaPrice, teaobj[xuhao].teaImg, bodyObj.num);
            carobj.push(tea)
            localStorage.Car=JSON.stringify(carobj);
            return true
        }
    }
})